var app = getApp(),
  that, sumintegral, shopstockId;
var Userintegral = (wx.getStorageSync('bizUserInfo').integral)
Page({
  data: {
    number: '',
    Integral: '',
    id: '',
    imgurl: '',
    Total: "",
    select: false,
    tihuoWay: '选择配送方式',
    tihuoWayid: '',
    shopimgname: '',
    // tab切换  
    currentTab: 0,
    dates: '请选择日期',
    custId: "",
    name: '',
    phone: '',
    shopData: '',
    price: '',
    userData: [],
    address: {},
    guigeid: ''
  },
  submintInfo: function() {
    that = this
    console.log(that.data.currentTab)
    if (that.data.currentTab == 0) {

      that.orderInfo()
      console.log(123)
    } else {
      if (that.data.address == null) {
        return wx.showModal({
          title: "提示",
          content: "请选择收货地址"
        })
      }
      that.deliveryInfo()
      console.log(555)
    }
  },
  orderInfo: function(e) {
    console.log(this.data.name)
    console.log(this.data.phone)
    if (this.data.name == null || this.data.name == "") {
      console.log('姓名')
      wx.showModal({
        title: "提示",
        content: "请输入收货人姓名"
      })
    } else if (this.data.phone == null || this.data.phone == "") {
      return wx.showModal({
        title: "提示",
        content: "请输入联系电话"
      })
    } else if (this.data.dates == '请选择日期' || this.data.dates == null || this.data.dates == '') {
      return wx.showModal({
        title: "提示",
        content: "请选择日期"
      })
    }else if (this.data.name && this.data.phone) {
      wx.request({
        // url: 'http://172.188.88.232:8080/order/submitOrder',
        url: app.api.submitOrder(),
        method: "POST",
        data: {
          "custId": that.data.custId,
          companyId: app.globalData.shopData.companyId,
          "ordType": 2,
          "totalOrdNum": that.data.number,
          "totalOrdPrice": that.data.Integral,
          "totalOrdAmt": that.data.Total,
          "amt": that.data.Total,
          "deliverType": 0,
          "ordPayType": 1,
          "orderAddr": {
            "rcverName": that.data.name,
            "rcverMobile": that.data.phone,
            "zipCode": '',
            "ordAddr1": '',
            "ordAddr2": ''
          },
          "orderGoodVoList": [{
            "goodId": that.data.id,
            "goodNum": that.data.number,
            "stockId": shopstockId
          }]
        },
        success: function(res) {
          var ordernumber = res.data.data
          console.log(Userintegral)
          console.log(sumintegral)
          if (Userintegral > sumintegral) {
            wx.navigateTo({
              url: `/page/mine/pages/dhsuccess/dhsuccess?id=${ordernumber}`,
            })
          } else {
            wx.showModal({
              title: "提示",
              content: res.data.message,
              showCancel: true, //是否显示取消按钮
            })
          }
        }
      })
      wx.setStorageSync('shopid', this.data.id)
    }
  },
  deliveryInfo: function() {
    that = this
    console.log(that.data.address)
    // var integar = wx.setStorageSync('bizUserInfo', bizUserInfo.integral)
    if (that.data.address == null && that.data.address == "") {
      wx.showModal({
        title: '提示',
        content: '请选择收货地址',
      })
    } else {
      wx.request({
        // url: 'http://172.188.88.232:8080/order/submitOrder',
        url: app.api.submitOrder(),
        method: "POST",
        data: {
          "custId": that.data.custId,
          companyId: app.globalData.shopData.companyId,
          "ordType": 2,
          "totalOrdNum": that.data.number,
          "totalOrdPrice": that.data.Integral,
          "totalOrdAmt": that.data.Total,
          "amt": that.data.Total,
          "deliverType": 0,
          "ordPayType": 1,
          "orderAddr": {
            "rcverName": that.data.address.hyName,
            "rcverMobile": that.data.address.telNumber,
            "zipCode": '',
            "ordAddr1": that.data.address.provinceName,
            "ordAddr2": that.data.address.detailInfo
          },
          "orderGoodVoList": [{
            "goodId": that.data.id,
            "goodNum": that.data.number,
            "stockId": shopstockId
          }]
        },
        success: function(res) {
          var ordernumber = res.data.data
          console.log(Userintegral)
          console.log(sumintegral)
          if (Userintegral > sumintegral) {

            wx.showModal({
              title: '提示',
              content: res.data.message,
              success: function(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: `/page/mine/pages/dhsuccess/dhsuccess?id=${ordernumber}`,
                  })
                } else {
                  console.log('取消')
                }
              }
            })
           
          } 
        }
      })
      wx.setStorageSync('shopid', that.data.id)
    }
  },
  keyName: function(t) {
    console.log(t)
    this.setData({
      name: t.detail.value
    })
  },
  keyPhone: function(t) {
    console.log(t)
    this.setData({
      phone: t.detail.value
    })
  },
  swichNav: function(e) {
    console.log(e);
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
    }
  },
  swiperChange: function(e) {
    console.log(e);
    this.setData({
      currentTab: e.detail.current,
    })

  },
  //  点击日期组件确定事件  
  bindDateChange: function(e) {
    console.log(e.detail.value)
    this.setData({
      dates: e.detail.value
    })
  },
  onLoad: function(options) {
    console.log(options.shopguigeid)
    console.log(options)
    shopstockId = options.shopguigeid
    this.setData({
      number: Number(options.number),
      Integral: Number(options.Integral),
      id: options.id,
      imgurl: options.imgurl,
      Total: Number(options.Integral) * Number(options.number),
      shopimgname: options.imgname,
      // guigeid: options.shoguigeid
    })
    sumintegral = Number(options.Integral) * Number(options.number) //实际支付
    // 生命周期函数--监听页面加载
  },

  onShow: function() {
    that = this
    // 获取门店信息
    var shopData = app.globalData.shopData
    var id = wx.getStorageSync("bizUserInfo").id
    that.setData({
      shopData: shopData
    }),
    console.log(id)
    console.log(shopData)
    // 生命周期函数--监听页面显示
   
    app.getData({
      url: app.api.selectUserAdd(),
      method: 'GET',
      data: {
        hyId: id
      },
      ok: function(res) {
        console.log(res.data.list[0])
        that.setData({
          custId: id,
          address: res.data.list[0]
        })
      }
    })

    // 获取用户地址
    app.getData({
      url: app.api.selectUserAdd(),
      method: 'GET',
      data: {
        hyId: id,
        isDefault: 1
      },
      ok: function (res) {
        console.log(res)
        if (res.data.list == null || res.data.list == '' || res.data.list.length == 0) {
          that.setData({
            address: null
          })
        } else {
          that.setData({
            custId: id,
            address: res.data.list[0]
          })
        }
      }
    })
  }
})